 /**
 * Description: 统计分析
 * @author:jacky
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-05-15   jacky       1.0.0     Initial
 */
package com.metro.demo.analysis.controller;


import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.excel.JxlsView;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.metro.demo.analysis.service.inf.IBizStatisticalAnalysisService;
import com.metro.demo.common.beans.constant.DicTypeEnum;
import com.metro.demo.common.beans.constant.JxlsTemplateEnum;
import com.metro.demo.common.utils.DicUtil;
import com.metro.demo.cust.beans.vo.BizCustomerManage;
import com.metro.demo.analysis.beans.vo.BizStatisticalAnalysis;
import com.metro.demo.web.controller.BaseController;
 /**
 * 统计分析<br>
 *
 * @author:jacky
 * @since:1.0.0
 * @version [1.0.0, 2018-05-15]
 */
@Controller
@RequestMapping("/bizStatisticalAnalysis")
public class BizStatisticalAnalysisController extends BaseController {
    //菜单编码
    private static final String menuCode = "BIZ_STATISTICAL_ANALYSIS";
    //模块描述
    private static final String MODULE_DESC = "统计分析";
    @Autowired
    private IBizStatisticalAnalysisService bizstatisticalanalysisService;
    @Override
    protected String getViewDir() {
        return "analysis/";
    }
    
    /**
     * 去查询统计分析
     * 
     * @param bizstatisticalanalysis 统计分析页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询统计分析列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(BizStatisticalAnalysis bizstatisticalanalysis) {
        ModelAndView mav = new ModelAndView();
        try {
        	mav.addObject("counts", DicUtil.getLabelValueList(DicTypeEnum.COUNT));
            mav.setViewName(getView("bizstatisticalanalysis_list"));
            mav.addObject("searchEntity", bizstatisticalanalysis);
        } catch (Exception ex) {
            log.error("查询统计分析出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询统计分析
     *
     * @param bizstatisticalanalysis 统计分析页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询统计分析列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(BizStatisticalAnalysis bizstatisticalanalysis, Page page, QueryParams<BizStatisticalAnalysis> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(bizstatisticalanalysis);
            PageInfo<BizStatisticalAnalysis> pageInfo = bizstatisticalanalysisService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询统计分析出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增统计分析
     * 
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增统计分析")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增统计分析出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改统计分析所需参数
     * @author jacky
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("action", action);
        mav.setViewName(getView("bizstatisticalanalysis_form"));
    }
    
    /**
     * 新增统计分析
     * 
     * @param entity 统计分析页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增统计分析")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveBizStatisticalAnalysis(BizStatisticalAnalysis entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = bizstatisticalanalysisService.save(entity);
            jr.setResult(res);
            log.debug("新增统计分析成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增统计分析出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除统计分析
     * 
     * @param id 统计分析页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除统计分析")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除统计分析id={}",id);
            IResult codeMsg = bizstatisticalanalysisService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除统计分析出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改统计分析
     * 
     * @param id 统计分析页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改统计分析")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            BizStatisticalAnalysis entity = bizstatisticalanalysisService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改统计分析", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改统计分析
     * 
     * @param entity 统计分析页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改统计分析")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateBizStatisticalAnalysis(BizStatisticalAnalysis entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = bizstatisticalanalysisService.update(entity);
            jr.setResult(res);
            log.debug("修改统计分析成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改统计分析出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去查看统计分析
     * 
     * @param id 统计分析页面表单对象唯一标识
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看统计分析详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", bizstatisticalanalysisService.getById(id));
            mav.setViewName(getView("bizstatisticalanalysis_info"));
        } catch (Exception ex) {
            log.error("查看统计分析出错", ex);
        }
        return mav;
        
    }
    
    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "export", method = RequestMethod.GET)
    public ModelAndView export(BizStatisticalAnalysis entity) {
        List<BizStatisticalAnalysis> list = bizstatisticalanalysisService.queryForList(entity);
        return new ModelAndView(new JxlsView(JxlsTemplateEnum.STATISTICANALYSIS, list));
    }
    
    /**
     * 报表Demo
     * 
     *
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2018-05-15]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看统计分析详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "chartDemo", method = { RequestMethod.GET })
    public ModelAndView chartDemo(BizStatisticalAnalysis entity) {
        ModelAndView mav= new ModelAndView();
        List<BizStatisticalAnalysis> list = bizstatisticalanalysisService.queryForList(entity);
        //拼接xml
        String strXML = "";
        strXML += "<graph caption='报表demo' xAxisName='小组' yAxisName='进厂量' decimalPrecision='0' formatNumberScale='0'>";
        for(BizStatisticalAnalysis demo : list ) {
        	//随机颜色
        	String r,g,b;  
        	Random random = new Random();  
            r = Integer.toHexString(random.nextInt(256)).toUpperCase();  
            g = Integer.toHexString(random.nextInt(256)).toUpperCase();  
            b = Integer.toHexString(random.nextInt(256)).toUpperCase();  
            r = r.length()==1 ? "0" + r : r ;  
            g = g.length()==1 ? "0" + g : g ;  
            b = b.length()==1 ? "0" + b : b ;  
            String color = r + g + b; 
            //以小组和进厂量测试demo
        	strXML += "<set name='"+demo.getImplLeader()+"' value='"+demo.getDayAmount()+"' color='"+color+"'/>";
        }
        strXML += "</graph>";
        try {
        	mav.addObject("strXML",strXML );
            mav.setViewName(getView("bzfusionCharts"));
        } catch (Exception ex) {
            log.error("查看统计分析出错", ex);
        }
        return mav;
      
    }
    
    //接口demo
   public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();  
		String errInfo = "success",str = "",rTime="";
		String url1 = "http://localhost:8181//appuser/getAppuserByUm.do?USERNAME=a1";
		try{
			long startTime = System.currentTimeMillis(); 					//请求起始时间_毫秒
			URL url = new URL(url1);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");		//请求类型  POST or GET	
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
			long endTime = System.currentTimeMillis(); 						//请求结束时间_毫秒
			String temp = "";
			while((temp = in.readLine()) != null){ 
				str = str + temp;
			}
			rTime = String.valueOf(endTime - startTime); 
		}
		catch(Exception e){
			errInfo = "error";
		}
		
		System.out.println("-----------------------"+str);
		System.out.println("-----------------------"+errInfo);
		System.out.println("-----------------------"+rTime);
		//map.put("errInfo", errInfo);//状态信息
		//map.put("result", str);		//返回结果
		//map.put("rTime", rTime);	//服务器请求时间 毫秒
		//return "";
	}
    
    
    
    
}
